package structures.tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode node1 = new HeroNode(1, "name1");
        HeroNode node2 = new HeroNode(2, "name2");
        HeroNode node3 = new HeroNode(3, "name3");
        HeroNode node4 = new HeroNode(4, "name4");
        HeroNode node5 = new HeroNode(5, "name5");
        binaryTree.setRoot(node1);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);

        binaryTree.preOrder();
        System.out.println("-------------------");

        binaryTree.infixOrder();
        System.out.println("-------------------");

        binaryTree.postOrder();
        System.out.println("-------------------");

        HeroNode preHeroNode = binaryTree.preOrderSearchById(5);
        if (preHeroNode != null) {
            System.out.println(preHeroNode);
        }
        System.out.println("--------前序遍历查找结束--------");

        HeroNode infixHeroNode = binaryTree.infixOrderSearchById(5);
        if (infixHeroNode != null) {
            System.out.println(infixHeroNode);
        }
        System.out.println("--------中序遍历查找结束--------");

        HeroNode postHeroNode = binaryTree.postOrderSearchById(5);
        if (postHeroNode != null) {
            System.out.println(postHeroNode);
        }
        System.out.println("--------后序遍历查找结束---------");


        System.out.println("--------删除结束 " + binaryTree.deleteById(5) + " ---------");
    }
}

class BinaryTree {
    HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void preOrder() {
        if (root != null) {
            root.preOrder();
        } else {
            System.out.println("root 为 null");
        }
    }

    public HeroNode preOrderSearchById(int id) {
        if (root != null) {
            return root.preOrderSearchById(id);
        }

        System.out.println("root 为 null");
        return null;
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("root 为 null");
        }
    }

    public HeroNode infixOrderSearchById(int id) {
        if (root != null) {
            return root.infixOrderSearchById(id);
        }

        System.out.println("root 为 null");
        return null;

    }

    public void postOrder() {
        if (root != null) {
            root.postOrder();
        } else {
            System.out.println("root 为 null");
        }
    }

    public HeroNode postOrderSearchById(int id) {
        if (root != null) {
            return root.postOrderSearchById(id);
        }

        System.out.println("root 为 null");
        return null;
    }


    /**
     * 通过id进行删除
     * @param id id
     * @return true/ false
     */
    public boolean deleteById(int id) {
        if (root == null) {
            return false;
        }

        if (root.getId() == id) {
            root = null;
            return true;
        }

        return root.deleteById(id);
    }
}

class HeroNode {
    private int id;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        System.out.println(this);
        if (this.getLeft() != null) {
            this.getLeft().preOrder();
        }

        if (this.getRight() != null) {
            this.getRight().preOrder();
        }
    }

    /**
     * 前序查找
     * @param id
     * @return
     */
    public HeroNode preOrderSearchById(int id) {
        HeroNode hero = null;
        if (this.getId() == id) {
            hero = this;
        }

        if (hero == null && this.getLeft() != null) {
            hero = this.getLeft().preOrderSearchById(id);
        }

        if (hero == null && this.getRight() != null) {
            hero = this.getRight().preOrderSearchById(id);
        }

        return hero;
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.getLeft() != null) {
            this.getLeft().infixOrder();
        }

        System.out.println(this);

        if (this.getRight() != null) {
            this.getRight().infixOrder();
        }
    }

    /**
     * 中序查找
     * @param id id
     * @return
     */
    public HeroNode infixOrderSearchById(int id) {
        HeroNode node = null;
        if (this.getLeft() != null) {
            node = this.getLeft().infixOrderSearchById(id);
        }

        if (node == null &&this.getId() == id) {
            node = this;
        }

        if (node == null && this.getRight() != null) {
            node = this.getRight().infixOrderSearchById(id);
        }

        return node;
    }

    /**
     * 后序遍历
     */
    public void postOrder() {
        if (this.getLeft() != null) {
            this.getLeft().postOrder();
        }

        if (this.getRight() != null) {
            this.getRight().postOrder();
        }

        System.out.println(this);
    }

    /**
     * 后续查找
     * @param id id
     * @return
     */
    public HeroNode postOrderSearchById(int id) {
        HeroNode node = null;
        if (this.getLeft() != null) {
            node = this.getLeft().postOrderSearchById(id);
        }

        if (node == null && this.getRight() != null) {
            node = this.getRight().postOrderSearchById(id);
        }

        if (node == null && this.getId() == id) {
            node = this;
        }
        return node;
    }

    public boolean deleteById(int id) {

        if (this.getLeft() != null && this.getLeft().getId() == id) {
            this.setLeft(null);
            return true;
        }

        if (this.getRight() != null && this.getRight().getId() == id) {
            this.setRight(null);
            return true;
        }

        if (this.getLeft() != null && this.getLeft().deleteById(id)) {
            return true;
        }

        if (this.getLeft() != null && this.getRight().deleteById(id)) {
            return true;
        }

        return false;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }


}
